import glob
import sys
import os
import shutil

BOARD_PATH = 'boards'
PACKAGES_PATH ='packages'
EXAMPLES_PATH ='examples'

PIKASCRIPT_PATH='components/pikapython'
PIKASCRIPT_LIB_PATH='components/pikapython/pikascript-lib'

def show_select(fs):
    n=0
    for f in fs:
        ss = f.split('.')
        board = ss[2]
        target=ss[3]
        print('%d %s %s' % (n, board, target))
        n+=1

def copy_examples(board, xfn):
    print('copy examples in %s' % xfn)
    print
    f=open(xfn)
    lines = f.readlines()
    for line in lines:
        line = line.strip()
        if line=='':
            continue
        if line.startswith('#'):
            continue
        # then it should be examples
        example_path = os.path.join(EXAMPLES_PATH, line)
        if os.path.isfile(example_path):
            if line.startswith(board):
                line =os.path.split(line)[1]
            dest_path = os.path.join(PIKASCRIPT_PATH, line)
            print('copy %s -> %s' % (example_path, dest_path))
            shutil.copyfile(example_path, dest_path)
                  
                                    
def copy_packages(pfn):
    print('copy packages in %s' % pfn)
    print
    f=open(pfn)
    lines = f.readlines()
    for line in lines:
        line =line.strip()
        if line=='':
            continue
        if line.startswith("#"):
            continue
        # then it should be package name
        package =line
        print('copy package ', package)
        lib_package_path = os.path.join(PIKASCRIPT_LIB_PATH, package)
        if not os.path.isdir(lib_package_path):
            os.makedirs(lib_package_path)
            
        package_path = os.path.join(PACKAGES_PATH, package)
        fs = os.listdir(package_path)
        for fn in fs:
            src_path = os.path.join(package_path, fn)            
            if fn.endswith('.py') or fn.endswith('.pyi') :
                dest_path = os.path.join(PIKASCRIPT_PATH, fn)
                print(' copy %s -> %s' % (src_path, dest_path))
                shutil.copyfile(src_path, dest_path)
            else:
                dest_path = os.path.join(lib_package_path, fn)
                print(' copy %s -> %s' % (src_path, dest_path))
                shutil.copyfile(src_path, dest_path)                


def main():
    boards = os.path.join(BOARD_PATH, 'sdkconfig.defaults.*.*')
    fs = glob.glob(boards)
    while 1:
        show_select(fs)
        k = input('Please select number(q=exit)>')
        if k=='q':
            sys.exit(0)
        try:
            n = int(k)
        except:
            print('invalid number\n')
            continue
        if n <0 or n >= len(fs):
            print('no such number\n')
            continue
        f = fs[n]
        ss = f.split('.')
        board = ss[2]
        target=ss[3]
        print('set board: %s' % board)
        # copy packages
        packages_fn = os.path.join(BOARD_PATH,'packages.%s' % board)
        if os.path.isfile(packages_fn):
            copy_packages(packages_fn)

        # copy examples
        examples_fn = os.path.join(BOARD_PATH, 'examples.%s' % board)
        if os.path.isfile(examples_fn):
            copy_examples(board, examples_fn)
            
        # copy sdkconfig to sdkconfig.defaults & run esp-idf set-target command
        shutil.copyfile(f, 'sdkconfig.defaults')
        os.system('idf.py set-target %s' % target)
        break
    
if __name__=='__main__':
    main()
        
